package com.facebook.games.fa.model
{
	import flash.utils.Dictionary;
	
	import mx.collections.ArrayCollection;
	
	/**
	 * Jogo
	 * @author Diego Maehler <dmaehler@gmail.com>
	 */
	[Bindable]
	public class Game
	{
		
		private var _playerOne : Player;
		
		private var _playerTwo : Player;
		
		private var rounds : ArrayCollection = new ArrayCollection();
		
		private var _isActive : Boolean = false;
		
		public var cardsDrawed : ArrayCollection = new ArrayCollection();
		
		private var _winner : Player;
		
		public function get winner () : Player
		{
			return this._winner;
		}
		
		public function get isActive () : Boolean
		{
			return this._isActive;
		}
		
		public function get playerTwo():Player
		{
			return _playerTwo;
		}

		public function set playerTwo(value:Player):void
		{
			_playerTwo = value;
		}

		public function get playerOne():Player
		{
			return _playerOne;
		}

		public function set playerOne(value:Player):void
		{
			_playerOne = value;
		}
		
		/**
		 * 
		 */
		public function Game (playerOne : Player = null, playerTwo : Player = null)
		{
			this.playerOne = playerOne;
			this.playerTwo = playerTwo;
		}
		
		/**
		 * Inicia o jogo, embaralhando as cartas
		 */
		public function startGame () : Game
		{
			this._isActive = true;
			this.playerOne.principalPack.shuffle();
			this.playerTwo.principalPack.shuffle();
			return this;
		}
		
		/**
		 * Finaliza o jogo atual, retornado o jogador vencedor, ou null caso empatou
		 * @return Winner
		 */
		public function endGame () : Player
		{
			this._winner 	= this.getWinner();
			this._isActive 	= false;
			return _winner;
		}
		
		/**
		 * Retorna o jogador vencedor, ou null caso empatou
		 * @return winner
		 */
		protected function getWinner () : Player
		{
			var cardsOne : int = this.playerOne.principalPack.cards.length + this.playerOne.aditionalPack.cards.length;
			var cardsTwo : int = this.playerTwo.principalPack.cards.length + this.playerTwo.aditionalPack.cards.length;
			
			if (cardsOne > cardsTwo) {
				return this.playerOne;
			} else if (cardsTwo > cardsOne) {
				return this.playerTwo;
			} else {
				return null;
			}
			
		}
		
		/**
		 * Inicia um novo round
		 * @return round
		 */
		public function startNewRound () : Round
		{
			
			if (this.isActive) {
				if (this.playerOne.principalPack.isEmpty()) {
					this.playerOne.transferPacks();
				}
				
				if (this.playerTwo.principalPack.isEmpty()) {
					this.playerTwo.transferPacks();
				}
				
				if (this.playerTwo.principalPack.isEmpty() || this.playerTwo.principalPack.isEmpty()) {
					return null;
				}
				
				const round : Round = new Round(this, this.rounds.length);
				
				round.start();
				
				this.rounds.addItem(round);
				
				return round;
				
			} else {
				
				return null;
				
			}
		}
		
		/**
		 * Retorna o último round ativo
		 * @return Round
		 */
		public function getLastRound () : Round
		{
			if (this.rounds.length > 0) {
				return this.rounds.getItemAt(this.rounds.length - 1) as Round;
			} else {
				return null;
			}
		}

	}
}